#include "Score.h"
#include "Money.h"

using namespace cocos2d;

Score::Score(CCLayer* layer)
{
	_layer = layer;
	_winSize = CCDirector::sharedDirector()->getWinSize();
	myMoney = 1000;

	sprintf(money, "%d", myMoney);
	label = CCLabelTTF::create(money, "Arial", 50);

	label->setColor(ccYELLOW);
	label->setPosition(ccp(_winSize.width/4, label->getContentSize().height/2));
	_layer->addChild(label, 4);
}


Score::~Score(void)
{
}

void Score::getMoney(CCPoint position, int get)
{
	// animation for money
	Money* pMoney = Money::create();
	pMoney->initWithFile("bullet02.png");
	pMoney->setPrice(get);

	pMoney->setPosition(position);
	_layer->addChild(pMoney, 5);
	CCFiniteTimeAction* actionMove = CCMoveTo::create((float)ccpDistance(position, label->getPosition())/800, label->getPosition());
	CCFiniteTimeAction* actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(Score::moveDone));

	CCRotateBy* actionBy = CCRotateBy::create(0.2f, 0.0f, -90.0f);
	CCRotateBy* actionByBack = (CCRotateBy*)actionBy->reverse();

	pMoney->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
	pMoney->runAction(CCSequence::create(actionBy, actionByBack, NULL));
}

void Score::costMoney(int cost)
{
	myMoney -= cost;
	sprintf(money, "%d", myMoney);
	label->setString(money);
}

void Score::moveDone(CCNode* sender)
{
	Money* pMoney = (Money*)sender;

	myMoney += pMoney->getPrice();
	sprintf(money, "%d", myMoney);
	label->setString(money);

	// play effect explosion
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("money.mp3");

	// show money
	char getMoney[10];
	sprintf(getMoney, "+%d", pMoney->getPrice());

	pMoney->initWithString(getMoney, "Arial", 50);
	pMoney->setColor(ccYELLOW);

	CCFiniteTimeAction* actionShowMoney = CCMoveTo::create(0.5, ccp(label->getPosition().x, label->getPosition().y+40));
	CCFiniteTimeAction* actionShowMoneyDone = CCCallFuncN::create(this, callfuncN_selector(Score::showMoneyDone));
	pMoney->runAction(CCSequence::create(actionShowMoney, actionShowMoneyDone, NULL));
}

void Score::showMoneyDone(CCNode* sender)
{
	Money* pMoney = (Money*)sender;

	_layer->removeChild(pMoney);
}